package com.xggstudio.crm.ui.mvp.p;

import android.util.Log;

import com.xggstudio.crm.api.APIServer;
import com.xggstudio.crm.base.App;
import com.xggstudio.crm.ui.mvp.m.BaseResult;
import com.xggstudio.crm.ui.mvp.m.LoginBean;
import com.xggstudio.crm.ui.mvp.v.ILogin;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class P_login {

    private ILogin login;

    public P_login(ILogin login) {
        this.login = login;
    }

    public void login(String name,String pwd){

        APIServer.getInstence().getServer()
                .login(name,pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("P_login", "onError: " + e.toString());
                    }

                    @Override
                    public void onNext(LoginBean loginBeanBaseResult) {
                        if(loginBeanBaseResult.getCode().equals("1")){

                            Log.e("P_login", "onNext: " + loginBeanBaseResult.toString());

                            App.getInstance().setLoginBean(loginBeanBaseResult);
                            login.toMain();
                        }else{
                            login.showWaring(loginBeanBaseResult.getMessage());
                        }
                    }
                });
    }
}
